
package combat.dudes.client.net.threads;

import gnu.cajo.utils.extra.TransparentItemProxy;

import java.util.ArrayList;

import combat.dudes.DudesFunctions;
import combat.dudes.client.ClientApp;
import combat.dudes.protocol.interfaces.PhysicalObjectInt;
import combat.dudes.protocol.interfaces.ProjectileInt;
import combat.dudes.protocol.interfaces.TrackInt;
import combat.dudes.protocol.interfaces.VehicleInt;


/**
 * @author Laurens Andriessen
 * @version 1.0
 */
public class NetSimulation {

	public NetSimulation() {
		ClientApp.SharedVars.addServer("localhost");

		ClientApp.SharedVars.addServer("192.168.123.321"); // Add test entry to serverlist, should be via multicast- or master server?

		// Create land
		/* Polygon poly = new Polygon();
		 * // Make a triangle
		 * poly.addPoint(100, 150);
		 * poly.addPoint(50, 200);
		 * poly.addPoint(150, 200);
		 * poly.addPoint(250, 200);
		 * poly.addPoint(200, 150);
		 * poly.addPoint(150, 100);
		 * poly.addPoint(100, 150);
		 * poly.addPoint(200, 150);
		 * poly.addPoint(150, 200);
		 * 
		 * ClientApp.SharedVars.setScenarioLand(poly); */


		// Temporary storage variables
		ArrayList<VehicleInt> vehicles = new ArrayList<VehicleInt>();
		ArrayList<ProjectileInt> projectiles = new ArrayList<ProjectileInt>();
		ArrayList<TrackInt> tracks = new ArrayList<TrackInt>();


		while(true) {

			long start = DudesFunctions.DateTime.getTimeInMs();

			if(ClientApp.SharedVars.sim != null) {

				if(ClientApp.SharedVars.getMyTeam() != null) {

					// Call the update_Remote()-method on the server and check if the size is >0
					if(ClientApp.SharedVars.sim.update_Remote(ClientApp.SharedVars.getMyTeam()).size() > 0) {
						vehicles.clear();
						projectiles.clear();
						tracks.clear();

						// Walk through all physical objects returned by the update_Remote-call
						for(Object physObj : ClientApp.SharedVars.sim.update_Remote(ClientApp.SharedVars.getMyTeam())) {
							PhysicalObjectInt obj = (PhysicalObjectInt) TransparentItemProxy.getItem(physObj, new Class[] { PhysicalObjectInt.class });
							if(obj != null)
								switch(obj.getType()) {
									case VEHICLE:
										vehicles.add((VehicleInt) TransparentItemProxy.getItem(physObj, new Class[] { VehicleInt.class }));
										break;
									case PROJECTILE:
										projectiles.add((ProjectileInt) TransparentItemProxy.getItem(physObj, new Class[] { ProjectileInt.class }));
										break;
									case TRACK:
										tracks.add((TrackInt) TransparentItemProxy.getItem(physObj, new Class[] { TrackInt.class }));
										break;
									case UNKNOWN:
										// Do nothing...
								break;
								}
						}

						// Put the filled arraylists in the SharedVars so the GUI-threads can access the found data
						ClientApp.SharedVars.setVehicles(vehicles);
						ClientApp.SharedVars.setProjectiles(projectiles);
						ClientApp.SharedVars.setTracks(tracks);
					}
				}
			}

			DudesFunctions.Status.DelayUpdate(start, 1000, Thread.currentThread());
		}
	}

	// Log in and set the SharedVars variables
	public static Boolean doLogOn(String username) {
		if(ClientApp.SharedVars.sim != null) {
			Object meRemote = ClientApp.SharedVars.sim.userLogOn_Remote(username);
			ClientApp.SharedVars.setMe(meRemote);
			if(ClientApp.SharedVars.getMe() != null) {
				ClientApp.Log("Logged in succesfully with username '" + username + "'");
				return true;
			} else {
				ClientApp.Log("Failed to log in with username '" + username + "'");
				return false;
			}
		}
		return false;
	}

}
